import time
import requests
from perf_settings import *
from common.hande_device import HandleDevices
from influxdb_client import Point

from common.handle_influxdb import InfluxDBHandler

do_devices = HandleDevices(CPDemo_pkgName=CPDemo_pkgName, SDKServer_pkgName=SDKServer_pkgName)
do_influxdb = InfluxDBHandler()

apm_pid_main_cpdemo, apm_pid_pid_att_cpdemo, apm_pid_main_server = do_devices.handle_main_device(
    deviceId_001=deviceId_001)
if Secondary_device:
    apm_pid_sec_cpdemo, apm_pid_sec_server = do_devices.handle_sec_device(deviceId_002=deviceId_002)

while True:
    # 准备数据点列表

    if Secondary_device:
        time.sleep(2)
        measurements = [
            Point("device_001").tag("pid", "main_cpdemo").field("cpu", do_devices.get_cpu(apm_pid_main_cpdemo)),
            Point("device_001").tag("pid", "att_cpdemo").field("cpu", do_devices.get_cpu(apm_pid_pid_att_cpdemo)),
            Point("device_001").tag("pid", "main_server").field("cpu", do_devices.get_cpu(apm_pid_main_server)),
            Point("device_001").tag("pid", "main_cpdemo").field("mem", do_devices.get_mem(apm_pid_main_cpdemo)),
            Point("device_001").tag("pid", "att_cpdemo").field("mem", do_devices.get_mem(apm_pid_pid_att_cpdemo)),
            Point("device_001").tag("pid", "main_server").field("mem", do_devices.get_mem(apm_pid_main_server)),
            # 分割
            Point("device_002").tag("pid", "sec_cpdemo").field("cpu", do_devices.get_cpu(apm_pid_sec_cpdemo)),
            Point("device_002").tag("pid", "sec_server").field("cpu", do_devices.get_cpu(apm_pid_sec_server)),
            Point("device_002").tag("pid", "sec_cpdemo").field("mem", do_devices.get_mem(apm_pid_sec_cpdemo)),
            Point("device_002").tag("pid", "sec_server").field("mem", do_devices.get_mem(apm_pid_sec_server)),
        ]
    else:
        time.sleep(2)
        measurements = [
            Point("device_001").tag("pid", "main_cpdemo").field("cpu", do_devices.get_cpu(apm_pid_main_cpdemo)),
            Point("device_001").tag("pid", "att_cpdemo").field("cpu", do_devices.get_cpu(apm_pid_pid_att_cpdemo)),
            Point("device_001").tag("pid", "main_server").field("cpu", do_devices.get_cpu(apm_pid_main_server)),
            Point("device_001").tag("pid", "main_cpdemo").field("mem", do_devices.get_mem(apm_pid_main_cpdemo)),
            Point("device_001").tag("pid", "att_cpdemo").field("mem", do_devices.get_mem(apm_pid_pid_att_cpdemo)),
            Point("device_001").tag("pid", "main_server").field("mem", do_devices.get_mem(apm_pid_main_server)),
        ]
    do_influxdb.write_mul_data(measurements=measurements)
